<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kafka 重平衡机制深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: shimmer 3s infinite;
        }
        
        @keyframes shimmer {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
            margin-bottom: 30px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 8px;
            width: 12px;
            height: 12px;
            background: #667eea;
            border-radius: 50%;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 20px;
            width: 2px;
            height: calc(100% + 10px);
            background: #e0e7ff;
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
        
        .impact-card {
            background: white;
            border-radius: 12px;
            padding: 24px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .impact-card:hover {
            border-color: #667eea;
            transform: translateY(-4px);
            box-shadow: 0 8px 30px rgba(102, 126, 234, 0.15);
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
        }
        
        .first-letter {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            color: #667eea;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Kafka 重平衡机制
                    <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">深度技术解析</span>
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    探索 Apache Kafka 如何通过智能重平衡机制，确保分布式系统的高可用性与负载均衡
                </p>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction Section -->
        <div class="content-card rounded-2xl shadow-xl p-8 md:p-12 mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">
                <i class="fas fa-balance-scale mr-3 text-purple-600"></i>什么是重平衡机制？
            </h2>
            <p class="text-lg text-gray-700 leading-relaxed mb-6">
                <span class="first-letter">K</span>afka的重平衡机制是指在Kafka消费者群体中发生变化时，系统会重新分配分区给消费者，以确保消费者群体内的负载均衡和数据的高可用性。这个过程是Kafka保证高吞吐量和数据可靠性的重要机制之一。
            </p>
            <div class="highlight-box">
                <p class="text-lg font-medium relative z-10">
                    <i class="fas fa-info-circle mr-2"></i>
                    当新的消费者加入消费者组、已有的消费者离开消费者组、或者消费者组内部发生其他变化时，Kafka就会触发重平衡机制。
                </p>
            </div>
        </div>

        <!-- Trigger Conditions -->
        <div class="content-card rounded-2xl shadow-xl p-8 md:p-12 mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-bolt mr-3 text-purple-600"></i>重平衡的触发条件
            </h2>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="impact-card">
                    <div class="feature-icon">
                        <i class="fas fa-user-plus"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800 mb-3">消费者组内有新消费者加入</h3>
                    <p class="text-gray-600">当新的消费者加入消费者组时，Kafka 将触发重平衡以重新分配分区，以确保新的消费者能够参与消息的消费。</p>
                </div>
                <div class="impact-card">
                    <div class="feature-icon">
                        <i class="fas fa-user-minus"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800 mb-3">消费者组内的消费者退出</h3>
                    <p class="text-gray-600">当消费者组内的某个消费者退出时，Kafka 也会触发重平衡，以重新分配该消费者所处理的分区给其他消费者。</p>
                </div>
                <div class="impact-card">
                    <div class="feature-icon">
                        <i class="fas fa-heartbeat"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800 mb-3">消费者组内的消费者心跳超时</h3>
                    <p class="text-gray-600">Kafka 通过心跳机制来检测消费者的健康状态，如果某个消费者在设定的超时时间内没有发送心跳，则被认为失效，触发重平衡。</p>
                </div>
                <div class="impact-card">
                    <div class="feature-icon">
                        <i class="fas fa-exchange-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800 mb-3">消费者组的消费者状态变化</h3>
                    <p class="text-gray-600">除了新消费者加入和消费者退出外，如果消费者组内的消费者状态发生变化，例如网络异常、消费者崩溃等，都可能触发重平衡机制。</p>
                </div>
            </div>
        </div>

        <!-- Consumer Group and Offset -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <div class="content-card rounded-2xl shadow-xl p-8">
                <h2 class="section-title text-2xl font-bold text-gray-800 mb-6">
                    <i class="fas fa-users mr-3 text-purple-600"></i>消费者组
                </h2>
                <p class="text-gray-700 leading-relaxed">
                    消费者组（Consumer Group）是一组逻辑上相互关联的消费者的集合，它们共同消费一个或多个主题（Topics）的消息。每个消费者组内的消费者共同消费主题下的消息，每个消息只会被消费者组内的一个消费者处理。
                </p>
            </div>
            <div class="content-card rounded-2xl shadow-xl p-8">
                <h2 class="section-title text-2xl font-bold text-gray-800 mb-6">
                    <i class="fas fa-map-marker-alt mr-3 text-purple-600"></i>消费者偏移量
                </h2>
                <p class="text-gray-700 leading-relaxed">
                    消费者偏移量（Consumer Offset）是指消费者在一个特定分区中已经消费到的消息的位置信息。Kafka中的每个分区都维护着消费者偏移量，记录了该分区下每个消费者已经消费的消息位置。
                </p>
            </div>
        </div>

        <!-- Rebalance Process -->
        <div class="content-card rounded-2xl shadow-xl p-8 md:p-12 mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-cogs mr-3 text-purple-600"></i>重平衡的执行过程
            </h2>
            <div class="space-y-4">
                <div class="timeline-item">
                    <h3 class="text-xl font-bold text-gray-800 mb-2">1. 检测触发条件</h3>
                    <p class="text-gray-600">Kafka 监听消费者组内的变化，当满足重平衡的触发条件时，将会启动重平衡流程。</p>
                </div>
                <div class="timeline-item">
                    <h3 class="text-xl font-bold text-gray-800 mb-2">2. 协调器选举</h3>
                    <p class="text-gray-600">Kafka 会选举一个消费者作为消费者组的协调器（Coordinator），负责协调重平衡过程。</p>
                </div>
                <div class="timeline-item">
                    <h3 class="text-xl font-bold text-gray-800 mb-2">3. 分配分区</h3>
                    <p class="text-gray-600">协调器首先获取当前的消费者组信息，包括消费者列表和分区分配情况。然后根据消费者列表和订阅的主题分区信息，计算出新的分区分配方案。</p>
                </div>
                <div class="timeline-item">
                    <h3 class="text-xl font-bold text-gray-800 mb-2">4. 分区重新分配</h3>
                    <p class="text-gray-600">基于计算出的新的分区分配方案，协调器将重新分配分区给消费者，确保每个消费者处理适当数量的分区，尽可能实现负载均衡。</p>
                </div>
                <div class="timeline-item">
                    <h3 class="text-xl font-bold text-gray-800 mb-2">5. 通知消费者</h3>
                    <p class="text-gray-600">协调器将新的分区分配方案通知给消费者组内的所有消费者。</p>
                </div>
                <div class="timeline-item">
                    <h3 class="text-xl font-bold text-gray-800 mb-2">6. 消费者重新分配消费者组内的分区</h3>
                    <p class="text-gray-600">接收